package com.mrzhou.job.service.impl;

import com.mrzhou.job.entity.SchedulerJob;
import com.mrzhou.job.mapper.SchedulerJobMapper;
import com.mrzhou.job.service.ISchedulerJobService;
import com.mrzhou.job.util.SchedulerUtil;
import com.mrzhou.mybatis.base.BaseServiceImpl;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SchedulerJobServiceImpl extends BaseServiceImpl<SchedulerJobMapper, SchedulerJob> implements ISchedulerJobService {

    @Autowired
    private Scheduler scheduler;

    @Transactional
    public void saveOrUpdateJob(SchedulerJob schedulerJob) {
        if (null == schedulerJob.getJobId()) {
            // 添加新的定时器任务
            this.saveOrUpdate(schedulerJob);
            // 创建定时器任务
            SchedulerUtil.createJob(scheduler, schedulerJob);
        } else {
            // 更新定时器任务
            this.updateById(schedulerJob);
            SchedulerUtil.updateJob(scheduler, schedulerJob);
        }

    }

}
